<template>
  <div>
    <el-table
      :data="displayData"
      style="width: 100%"
      @row-click="handleRowClick">
      <el-table-column
        prop="Id"
        label="Id">
      </el-table-column>

      <el-table-column
        prop="Name"
        label="Name">
      </el-table-column>

      <el-table-column
        prop="Gold"
        label="Gold">
      </el-table-column>

      <el-table-column
        prop="Gem"
        label="Gem">
      </el-table-column>

      <el-table-column
        prop="Level"
        label="Level"
        width="180">
      </el-table-column>

      <el-table-column
        prop="Exp"
        label="Exp"
        width="180">
      </el-table-column>

      <el-table-column
        prop="Server"
        label="Server"
        width="180">
      </el-table-column>

      <el-table-column
        prop="Created"
        label="Created"
        width="180">
      </el-table-column>

      <el-table-column
        prop="Updated"
        label="Updated"
        width="180">
      </el-table-column>

      <el-table-column label="Actions" align="center">
        <template #default="scope">
          <el-button @click="handleEdit(scope.row)">
            Edit
          </el-button>
          <el-button @click="handleDelete(scope.row)">
            Delete
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑对话框 -->
    <el-dialog
      title="编辑数据"
      v-model="editDialogVisible"
      width="30%">
      <el-form :model="form" label-width="80px">
        <el-form-item label="Id">
          <el-input v-model="form.Id" disabled></el-input>
        </el-form-item>
        <el-form-item label="Name">
          <el-input v-model="form.Name"></el-input>
        </el-form-item>
        <el-form-item label="Gold">
          <el-input v-model.number="form.Gold"></el-input>
        </el-form-item>
        <el-form-item label="Gem">
          <el-input v-model.number="form.Gem"></el-input>
        </el-form-item>
        <el-form-item label="Level">
          <el-input v-model.number="form.Level"></el-input>
        </el-form-item>
        <el-form-item label="Exp">
          <el-input v-model.number="form.Exp"></el-input>
        </el-form-item>
        <el-form-item label="Server">
          <el-input v-model.number="form.Server"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmEdit">确认</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 删除确认对话框 -->
    <el-dialog
      title="提示"
      v-model="deleteDialogVisible"
      width="30%">
      <span>确定要删除这条数据吗？</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="deleteDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmDelete">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { userinfo } from '@/api/user'
export default {
  name: 'Userinfo',
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      displayData: [], // 表格数据，假设从接口获取
      editDialogVisible: false, // 编辑对话框显示状态
      deleteDialogVisible: false, // 删除确认对话框显示状态
      form: {}, // 编辑表单数据
      currentRow: null // 当前操作的行数据
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      userinfo().then((res) => {
        console.log(res)
        this.tableData = res.data
        this.total = this.tableData.length
        this.handlePageChange()
      }).catch((err) => {
        console.log(err)
      })
    },
    handlePageChange() {
      const startIndex = (this.currentPage - 1) * this.pageSize
      const endIndex = startIndex + this.pageSize
      this.displayData = this.tableData.slice(startIndex, endIndex)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.handlePageChange()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.handlePageChange()
    },
    // 处理编辑按钮点击
    handleEdit(row) {
      this.currentRow = row
      this.form = { ...row }
      this.editDialogVisible = true
    },
    // 确认编辑
    confirmEdit() {
      // 这里可以发送请求到后端更新数据
      // 假设更新成功，更新表格中的数据
      const index = this.displayData.findIndex(item => item.Id === this.currentRow.Id)
      if (index !== -1) {
        this.displayData.splice(index, 1, this.form)
      }
      this.editDialogVisible = false
    },
    // 处理删除按钮点击
    handleDelete(row) {
      this.currentRow = row
      this.deleteDialogVisible = true
    },
    // 确认删除
    confirmDelete() {
      // 这里可以发送请求到后端删除数据
      // 假设删除成功，从表格中移除数据
      this.displayData = this.displayData.filter(item => item.Id !== this.currentRow.Id)
      this.deleteDialogVisible = false
    },
    // 处理行点击（可选，若有需要）
    handleRowClick(row) {
      this.currentRow = row
    }
  }
}
</script>

<style scoped>
/* 可根据需要添加样式 */
</style>
